package com.q.tqb.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;
import com.q.tqb.R;
import com.q.tqb.adapter.TweeterAdapter;
import com.q.tqb.base.AppConst;
import com.q.tqb.model.RankInfo;
import com.q.tqb.model.TweeterAmountInfo;
import com.q.tqb.util.LogUtil;
import com.q.tqb.util.MD5Util;
import com.q.tqb.util.SPUtil;
import com.q.tqb.util.StringUtil;
import com.q.tqb.util.http.IResponse;
import com.q.tqb.util.http.OkHttpUtil;
import com.q.tqb.view.refresh.GRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class TweeterFragment extends Fragment {

    @BindView(R.id.rv_mine_tweeter)
    GRecyclerView mRvTweeter;

    Unbinder unbinder;


    @BindView(R.id.tv_amount1)
    TextView mTvAmount1;
    @BindView(R.id.tv_amount2)
    TextView mTvAmount2;
    @BindView(R.id.tv_amount3)
    TextView mTvAmount3;
    @BindView(R.id.tv_amount4)
    TextView mTvAmount4;



    private ArrayList<RankInfo> mGrabRedDatas;
    private TweeterAdapter mTweeterAdapter;
    private int currentPageNo;

    private int uid;


    public TweeterFragment() {
        // Required empty public constructor
    }

    public static TweeterFragment newInstance() {
        TweeterFragment fragment = new TweeterFragment();
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_tweeter, container, false);

        unbinder = ButterKnife.bind(this, view);
        uid = (int) SPUtil.get(getActivity(), "uid", 0);
        init();
        getAmount();
        return view;
    }

    private void init() {
        mGrabRedDatas = new ArrayList<>();
        mRvTweeter.setLoadingMoreEnabled(true);
        mRvTweeter.setLoadingListener(new GRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                currentPageNo = 1;
                mRvTweeter.setNoMore(false);
                mRvTweeter.setLoadingMoreEnabled(true);
                getListData();
            }

            @Override
            public void onLoadMore() {
                currentPageNo++;
                getListData();
            }
        });
        mTweeterAdapter = new TweeterAdapter(getActivity(), mGrabRedDatas);
        LinearLayoutManager linearLayoutManagert = new LinearLayoutManager(getActivity());
        mRvTweeter.setLayoutManager(linearLayoutManagert);
        mRvTweeter.setRefresh();
        mRvTweeter.setAdapter(mTweeterAdapter);
    }


    private void getAmount() {
        Map<String, String> map = new HashMap<>();
        map.put("uid", uid + "");
        map.put("token", StringUtil.getToken(uid));

        OkHttpUtil.getInstance().post(getActivity(), AppConst.URL_GET_USER_AGENT_NUM, map, new IResponse() {
            @Override
            public void onSuccess(JsonElement element) {
                LogUtil.i(element.toString());
                Gson gson = new Gson();
                TweeterAmountInfo amountInfo = gson.fromJson(element, TweeterAmountInfo.class);
                mTvAmount1.setText(amountInfo.getUserNum() + "位");
                mTvAmount2.setText(amountInfo.getUserAgentNum() + "位");
                mTvAmount3.setText(amountInfo.getTodayUserNum() + "位");
                mTvAmount4.setText(amountInfo.getTodayUserAgentNum() + "位");
            }

            @Override
            public void onFailure(int statusCode, String errorMsg) {
                LogUtil.i(errorMsg + "statusCode" + statusCode);
                Toast.makeText(getActivity(), errorMsg, Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void getListData() {
        Map<String, String> map = new HashMap<>();
        map.put("uid", uid + "");
        map.put("token", StringUtil.getToken(uid));
        map.put("pageNo", currentPageNo + "");
        map.put("pageSize ", 10 + "");
        OkHttpUtil.getInstance().post(getActivity(), AppConst.URL_GET_TWEETER_USER, map, new IResponse() {
            @Override
            public void onSuccess(JsonElement element) {
                LogUtil.i(element.toString());
                Gson gson = new Gson();
                ArrayList<RankInfo> grabRedDatas = gson.fromJson(element, new TypeToken<ArrayList<RankInfo>>() {
                }.getType());
                if (currentPageNo == 1) {
                    mGrabRedDatas.clear();
                    mRvTweeter.refreshComplete();
                    if (grabRedDatas.size() < 10) {
                        mRvTweeter.setNoMore(true);
                    }
                } else {
                    if (grabRedDatas.size() < 10) {
                        mRvTweeter.setNoMore(true);
                    } else {
                        mRvTweeter.loadMoreComplete();
                    }
                }
                mGrabRedDatas.addAll(grabRedDatas);
                mTweeterAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(int statusCode, String errorMsg) {
                if (currentPageNo == 1) {
                    mRvTweeter.refreshComplete();
                } else {
                    mRvTweeter.loadMoreComplete();
                }
                LogUtil.i(errorMsg + "statusCode" + statusCode);
                Toast.makeText(getActivity(), errorMsg, Toast.LENGTH_SHORT).show();
            }
        });
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

}
